package com.ruoyi.graduate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.graduate.domain.entity.MsDelog;
import com.ruoyi.graduate.domain.entity.MsPhotographyOrder;
import com.ruoyi.graduate.domain.param.*;
import com.ruoyi.graduate.domain.vo.MsOrderPageVo;
import com.ruoyi.graduate.service.IMsDelogService;
import com.ruoyi.graduate.service.MsPhotographyOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 摄影订单控制接口
 */
@RestController
@RequestMapping("/api/order")
@Api(tags = "摄影订单控制接口")
public class MsPhotographyOrderController extends BaseController {

    @Autowired
    private MsPhotographyOrderService msPhotographyOrderService;

    @Autowired
    private IMsDelogService imsDelogService;


    @ApiOperation("订单分页查询")
    @GetMapping
    public TableDataInfo pageList(MsPhotographyOrderPageParam param){

        PageInfo pageInfo = msPhotographyOrderService.pageList(param);

        return getDataTable(pageInfo.getList(), pageInfo.getTotal());
    }


    @ApiOperation("查询订单详情")
    @GetMapping("/{orderId}")
    public AjaxResult detail(@PathVariable("orderId") Integer orderId){

        return msPhotographyOrderService.detail(orderId);
    }


    @ApiOperation("订单删除")
    @DeleteMapping("/{orderId}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delete(@PathVariable("orderId") Integer orderId){
        QueryWrapper qw = new QueryWrapper();
        qw.eq("order_id", orderId);
        MsPhotographyOrder byId = msPhotographyOrderService.getById(orderId);
        if (byId != null && byId.getOrderStatus() != 2) {
            boolean b = msPhotographyOrderService.removeById(orderId);

            MsDelog delog = new MsDelog();
            delog.setTitle("订单删除");
            delog.setContent("您的订单" + byId.getOrderNo() + "状态为删除");
            delog.setCustomerId(byId.getCustomerId());

            imsDelogService.save(delog);

            return b ? AjaxResult.success() : AjaxResult.error();
        }

        return AjaxResult.error("删除失败！订单被接单，现在不能删除！");
    }

    @ApiOperation("摄影师查询自身相关订单")
    @GetMapping("/photoOrder")
    public TableDataInfo photoOrderList(MsOrderPageParam param){

        PageInfo pageInfo = msPhotographyOrderService.photoPageList(param);

        return getDataTable(pageInfo.getList(), pageInfo.getTotal());
    }


    @ApiOperation("摄影师接/拒单")
    @PutMapping("/recevice")
    public AjaxResult recevice(@ModelAttribute  MsPhotographyOrderReceiveParam param){

        return msPhotographyOrderService.photoRecevive(param);
    }



    @ApiOperation("订单自主选择新增")
    @PostMapping("/orderMy")
    public AjaxResult orderMy(@RequestBody  MsOrderUserAddParam param){

        System.out.println(param);


//        return AjaxResult.success();
        return msPhotographyOrderService.orderUserAdd(param);
    }


    @ApiOperation("订单套餐新增")
    @PostMapping("/orderPackage")
    public AjaxResult orderPackage(@RequestBody MsOrderPackageAddParam param){

        return msPhotographyOrderService.orderPackageAdd(param);
    }


    @ApiOperation("订单列表获取")
    @GetMapping("/orderlist/{photograId}")
    public AjaxResult orderlist(@PathVariable("photograId") Long photograId){

        return msPhotographyOrderService.listVo(photograId);
    }
}
